package com.jp.proxy;

import android.app.Application;

import com.jp.proxy.httpprocessor.HttpHelper;
import com.jp.proxy.httpprocessor.IHttpProcessor;
import com.jp.proxy.httpprocessor.OkHttpProcessor;

import java.lang.reflect.Proxy;

/**
 * @ProjectName : HttpProcessor_20200413
 * @Author : Jason
 * @Time : 2021/8/29 16:31
 * @Description : 文件描述
 */
public class MyApplication extends Application {

    private IHttpProcessor iHttpProcessor;

    @Override
    public void onCreate() {
        super.onCreate();
        //静态代理切换需要代理的网络框架
        OkHttpProcessor okHttpProcessor = new OkHttpProcessor();
        HttpHelper.getInstance().init(okHttpProcessor);
        /*HttpHelper.getInstance().init(new VolleyProcessor(this));
        HttpHelper.getInstance().init(new XUtilsProcessor(this));
        HttpHelper.getInstance().init(new OtherProcessor());*/
        dynamicProxyNetUtils(okHttpProcessor);
    }

    public IHttpProcessor getHttpProcessor() {
        return iHttpProcessor;
    }

    /**
     * 动态代理网络框架
     *
     * @param okHttpProcessor
     */
    private void dynamicProxyNetUtils(OkHttpProcessor okHttpProcessor) {
        //动态代理
        iHttpProcessor = (IHttpProcessor) Proxy.newProxyInstance(okHttpProcessor.getClass().getClassLoader(), okHttpProcessor.getClass().getInterfaces(), (proxy, method, args) -> {
            method.invoke(okHttpProcessor, args);
            return null;
        });
    }
}
